package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.mapper.PermissionMapper;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.util.PermissionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }


    @Override
    public Permission get(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> listAll() {
        return permissionMapper.selectAll();
    }


    /**
     * 分页查询
     *
     * @param qo 封装查询时需要的数据#{xxx}来取其属性值
     * @return
     */
    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        //查询结果集
        List<Permission> list = permissionMapper.selectForList(qo);
        return new PageInfo(list);
    }

    //获得Spring的上下文对象
    @Autowired
    private ApplicationContext ctx;

    //重新加载的方法
    @Override
    public void reload() {
        //查询获取数据库中的所有的权限表达式
        List<String> allExpression = permissionMapper.selectAllExpression();

        //获取带有某个注解的bean，获取所有的controller
        Map<String, Object> beans = ctx.getBeansWithAnnotation(Controller.class);
        Collection<Object> controllers = beans.values();
        for (Object controller : controllers) {
            //获取字节码对象
            Class<?> clazz = controller.getClass().getSuperclass();
            //获取没个controller中的每一个方法
            Method[] methods = clazz.getDeclaredMethods();
            for (Method method : methods) {
                //判断方法是否贴有Shiro的权限注解
                RequiresPermissions annotation = method.getAnnotation(RequiresPermissions.class);
                //有贴注解，就报存权限到数据库中
                if (annotation != null) {
                    String expression = annotation.value()[0];//从shiro注释中拿第0个元素
                    //如果该权限表达式不存在数据库中就插入，否则不插入
                    if (!allExpression.contains(expression)) {
                        Permission permission = new Permission();
                        permission.setName(annotation.value()[1]);//拿到注解里的value的第二个值，让表达式设置到名字
                        permission.setExpression(expression);//设置拼接的表达式
                        permissionMapper.insert(permission);

                    }
                }

            }

        }
    }

    /**
     * 根据用户查询权限的表达式
     *
     * @param employee
     * @return
     */
    @Override
    public List<String> selectByEmpId(Employee employee) {
        List<String> permissions = permissionMapper.selectByEmpId(employee);
        return permissions;
    }
}
